<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<head>
<base href="<%=basePath%>">
<title>user_management</title>
<link rel="stylesheet" type="text/css"
	href="js/ext/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="style/mystyle.css" />
<script type="text/javascript" src="js/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="js/ext/ext-all.js"></script>
<script>
	Ext.onReady(function() {

		var treeRow_1 = "null";
		var treeRow_2 = "null";
		var parastring = "null";
		var username = "null";

		var privligePanel = new Ext.tree.TreePanel({
			title : '权限树',
			region : 'center',
			frame : true,
			margins : '5 5 0 5',
			autoScroll : true,
			//bodyStyle:'background:#f1f1f1',
			//html:'PRIVILEGE PANEL, CREATE A PRIVLIEGE AND THEN CHOISE A MODULE OR JUST WRITE A URL TO THIS PRIVILEGE',
			height : 300,
			width : 400,
			loader : new Ext.tree.TreeLoader({
				dataUrl : 'privilege.svt?kind=privilegetree'
			}),
			root : new Ext.tree.AsyncTreeNode({
				text : 'Privilege'
			}),
			columns : [{
				header : 'Task',
				width : 100,
				dataIndex : 'Privilege'
			}]
		});

		privligePanel.on('click', function(node) {
			if (node.id != 'root') {
				treeRow_1 = node.text;
// 				alert(treeRow_1);
			}
		});

		var editPanel = new Ext.FormPanel({
			title : '编辑面板',
			region : 'east',
			frame : true,
			margins : '5 5 0 0',
			width : 500,
			tbar : [
					new Ext.Button({
						text : '添加',
						handler : function() {

							if (Ext.getCmp("discript").getValue() == ""
									|| Ext.getCmp("id").getValue() == ""
									|| Ext.getCmp("url").getValue() == ""
									|| Ext.getCmp("parent").getValue() == "") {
								Ext.MessageBox.alert("Prompt",
										"Please fill data.");
								return;
							}

							var requestConfig = {
								url : 'privilege.svt?kind=add',
								params : {
									discript : Ext.getCmp("discript")
											.getValue(),
									url : Ext.getCmp("url").getValue(),
									id : Ext.getCmp("id").getValue(),
									parent : Ext.getCmp("parent").getValue()
								},
								callback : function(option, success, response) {
									var msg = [success, option.customer,
											response.responseText];
									if(response.responseText=="1"){
										privligePanel.getRootNode().reload();
									}
								}
							}
							Ext.Ajax.request(requestConfig);

						}
					}),
					{
						xtype : 'tbseparator'
					},
					new Ext.Button({
						text : '删除',
						handler : function() {
							if (treeRow_1 == "null") {
								Ext.MessageBox.alert("Prompt",
										"Please choose one privilege.");
								return;
							}
							var requestConfig = {
								url : 'privilege.svt?kind=delete',
								params : {
									parastring : treeRow_1
								},
								callback : function(option, success, response) {
									var msg = [success, option.customer,
											response.responseText];
									if(response.responseText=="1"){
										privligePanel.getRootNode().reload();
									}
								}
							}
							Ext.Ajax.request(requestConfig);
						}
					})
			// 			,{
			// 						xtype : 'tbseparator'
			// 					},
			// 			new Ext.Button({
			// 				text:'Update'
			// 			})
			],
			items : [{
				fieldLabel : '权限名',
				xtype : 'textfield',
				name : 'discript',
				width : 300,
				id : 'discript'
			}, {
				fieldLabel : '父权限ID',
				xtype : 'textfield',
				name : 'parent',
				width : 300,
				id : 'parent'
			}, {
				fieldLabel : 'URL',
				xtype : 'textfield',
				name : 'url',
				width : 300,
				id : 'url'
			}, {
				fieldLabel : 'ID',
				xtype : 'textfield',
				name : 'id',
				width : 300,
				id : 'id'
			}]
		});

		var container_2 = new Ext.Panel({
			frame : true,
			region : 'north',
			margins : '5 5 0 5',
			layout : 'border',
			height : 200,
			items : [privligePanel, editPanel]
		})

		var asignPanel = new Ext.tree.TreePanel({
			region : 'west',
			frame : true,
			title : '派发',
			autoScroll : true,
			margins : '0 0 5 5',
			//bodyStyle:'background:#f1f1f1',
			//html:'PRIVILEGE PANEL, FIRSTLY CHOSE A PRIVILEGE, THEN CHOISE A USER',
			width : 613,
			loader : new Ext.tree.TreeLoader({
				dataUrl : 'privilege.svt?kind=privilegetree'
			}),
			root : new Ext.tree.AsyncTreeNode({
				text : 'Chose privilege'
			})
		});

		asignPanel.on('click', function(node) {
			if (node.id != 'root') {
				treeRow_2 = node.text;
// 				alert(treeRow_2);
			}
		});

		var proxy = new Ext.data.HttpProxy({
			url : 'usermanagement.svt?kind=user'
		});
		//定义reader
		var reader = new Ext.data.JsonReader({}, [{
			name : 'username'
		}, {
			name : 'gender'
		}, {
			name : 'usergroup'
		}, {
			name : 'comments'
		}]);
		//构建Store   
		var store = new Ext.data.Store({
			proxy : proxy,
			reader : reader
		});
		//载入
		store.load();

		var userPanel = new Ext.grid.GridPanel({
			store : store,
			id : 'users',
			title : "用户",
			region : 'center',
			margins : '0 0 5 5',
			width : 400,
			tbar : [
					new Ext.Button({
						text : '授权用户',
						handler : function() {
							if (username == "null" ||treeRow_2 == "null") {
								Ext.MessageBox.alert("Prompt",
										"Please choose one privilege and a user.");
								return;
							}

							var requestConfig = {
								url : 'privilege.svt?kind=grant',
								params : {
									username:username,
									parastring:treeRow_2
								},
								callback : function(option, success, response) {
									if(response.responseText == "1"){
										Ext.MessageBox.alert("Prompt","Success!");
									}else{
										Ext.MessageBox.alert("Prompt","Failed!");
									}
								}
							}
							Ext.Ajax.request(requestConfig);

						}
					}), {
						xtype : 'tbseparator'
					}, new Ext.Button({
						text : '回收权限',
						handler:function(){
						
							if (username == "null" ||treeRow_2 == "null") {
								Ext.MessageBox.alert("Prompt",
										"Please choose one privilege and a user.");
								return;
							}

							var requestConfig = {
								url : 'privilege.svt?kind=strip',
								params : {
									username:username,
									parastring:treeRow_2
								},
								callback : function(option, success, response) {
									var msg = [success, option.customer,
											response.responseText];
									if(response.responseText == "1"){
										Ext.MessageBox.alert("Prompt","Success!");
									}else{
										Ext.MessageBox.alert("Prompt","Failed!");
									}
								}
							}
							Ext.Ajax.request(requestConfig);
					}})],
			columns : [{
				id : 'username',
				header : 'username',
				width : 130,
				sortable : true,
				dataIndex : 'username',
				columnIndex : 'username'
			}, {
				header : 'gender',
				width : 120,
				sortable : true,
				dataIndex : 'gender',
				columnIndex : 'gender'
			}, {
				header : 'usergroup',
				width : 130,
				sortable : true,
				dataIndex : 'usergroup',
				columnIndex : 'usergroup'
			}, {
				header : 'comments',
				width : 120,
				sortable : true,
				dataIndex : 'comments',
				columnIndex : 'comments'
			}]
		});

		/***********************************************
		          Listeners
		 ***********************************************/
		userPanel.addListener('cellclick', cellclick);

		function cellclick(grid, rowIndex, columnIndex, e) {
			var record = grid.getStore().getAt(rowIndex); //Get the Record   
			var selectRows = grid.getSelectionModel().getSelections();
			username = selectRows[0].get("username");
// 			alert(username);

			// 			Ext.MessageBox.alert('show', '当前选中的数据是' + username + gender
			// 					+ usergroup + comments);
		}

		var c_panel = new Ext.Panel({
			layout : 'border',
			frame : true,
			region : 'center',
			margins : '5 5 5 5',
			items : [asignPanel, userPanel]
		});

		var viewport = new Ext.Viewport({
			layout : 'border',
			items : [container_2, c_panel]
		});
	});
</script>
</head>
<body>
</body>
</html>
